<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8" %>
<%@ page trimDirectiveWhitespaces="true" %>
<%@ page isELIgnored="false" %>
<%@ taglib prefix="c" uri="jakarta.tags.core" %>
<%@ taglib prefix="ujn" uri="http://ujn.edu.cn/common/" %>
<%
  String path = request.getContextPath();
  String basePath = request.getScheme() + "://" + request.getServerName()
          + ":" + request.getServerPort() + path + "/";
%>
<!DOCTYPE HTML>
<html>

<head>
  <title>用户信息</title>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  <!-- 引入css样式文件 -->
  <!-- Bootstrap Core CSS -->
  <link href="<%=basePath%>css/bootstrap.css" rel="stylesheet"/>
  <!-- Bootstrap-grid CSS -->
  <link href="<%=basePath%>css/bootstrap-grid.css" rel="stylesheet"/>
  <!-- Bootstrap-reboot CSS -->
  <link href="<%=basePath%>css/bootstrap-reboot.css" rel="stylesheet"/>
  <!-- MetisMenu CSS -->
  <link href="<%=basePath%>css/metisMenu.min.css" rel="stylesheet" />
  <!-- DataTables CSS -->
  <link href="<%=basePath%>css/dataTables.bootstrap.css" rel="stylesheet" />
  <!-- Custom CSS -->
  <link href="<%=basePath%>css/sb-admin-2.css" rel="stylesheet" />
  <!-- Custom Fonts -->
  <link href="<%=basePath%>css/font-awesome.min.css" rel="stylesheet" type="text/css" />
  <link href="<%=basePath%>css/boot-crm.css" rel="stylesheet" type="text/css" />
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
</head>
<body>

<div>
  <nav class="navbar navbar-expand-lg navbar-dark " style="background-color:#81D8CF;;">
    <img src="imges/logo.jpg" class="rounded float-left" alt="Logo" height="45" width="45">
    <div class="container" style="">
      <a class="navbar-brand" href="#">停车场管理系统</a>
      <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarText"
              aria-controls="navbarText" aria-expanded="false" aria-label="Toggle navigation">
        <span class="navbar-toggler-icon"></span>
      </button>
      <div class="collapse navbar-collapse" id="navbarText">
        <ul class="navbar-nav mr-auto">
          <li class="nav-item">
            <a class="nav-link active" href="${pageContext.request.contextPath }/user">用户信息</a>
          </li>
          <li class="nav-item">
            <a class="nav-link " href="${pageContext.request.contextPath }/book">预定信息</a>
          </li>
          <li class="nav-item">
            <a class="nav-link" href="${pageContext.request.contextPath }/parking_record">车辆进出</a>
          </li>
          <li class="nav-item">
            <a class="nav-link " href="${pageContext.request.contextPath }/parking_space_manage">停车位管理</a>
          </li>
          <li class="nav-item">
            <a class="nav-link" href="${pageContext.request.contextPath }/rate">收费标准</a>
          </li>
          <li class="nav-item">
            <a class="nav-link" href="${pageContext.request.contextPath }/adminequip">设备管理</a>
          </li>
          <li class="nav-item">
            <a class="nav-link" href="${pageContext.request.contextPath}/vehicle">车辆信息</a>
          </li>
        </ul>
        <span class="navbar-text">
          导航栏
        </span>
      </div>
    </div>
  </nav>
</div>
<%

  if (session.getAttribute("USER_SESSION")!=null) {
%>
<!--登录成功界面-->




<div class="container">
  <div class="p-2 bg-light border">
    <div class="row">

      <div class="panel panel-default">
        <div class="panel-body">
          <form class="form-inline" method="get"
                action="${pageContext.request.contextPath }/user">
            <div class="form-group">
              <label for="username">&nbsp;&nbsp;&nbsp;用户名</label>
              <input type="text" class="form-control" id="username"
                     value="${username}" name="username"/>
            </div>
            <div class="form-group">
              <label for="password">密码</label>
              <input type="text" class="form-control" id="password"
                     value="${password}" name="password"/>
            </div>
            <div class="form-group">
              <label for="phone">手机号</label>
              <input type="text" class="form-control" id="phone"
                     value="${phone}" name="phone"/>
            </div>
            <div class="form-group">
              <label for="userType">用户类型</label>
              <select name="userType" id="userType" class="form-control" value="${userType}">
                <option value=""></option>
                <option value="普通用户">普通用户</option>
                <option value="VIP用户">VIP用户</option>
                <option value="管理员">管理员</option>
              </select>
            </div>
            <button type="submit" class="btn btn-primary">查询</button>
          </form>
        </div>
      </div>
    </div>
  </div>
  <a href="#" class="btn btn-primary" data-toggle="modal"
     data-target="#newUserDialog" onclick="clearUser()">新建</a>
  <div class="p-2 bg-light border">
    <table class="table table-striped">
      <thead>
      <tr class="text-center">
        <th>编号</th>
        <th>姓名</th>
        <th>密码</th>
        <th>手机号</th>
        <th>用户类型</th>
        <th>操作</th>
      </tr>
      </thead>
      <tbody>
      <c:forEach items="${page.rows}" var="row">
        <tr class="text-center">
          <td>${row.userId}</td>
          <td>${row.username}</td>
          <td>${row.password}</td>
          <td>${row.phone}</td>
          <td>${row.userType}</td>
          <td>
            <a href="#" class="btn btn-primary btn-xs" data-toggle="modal" data-target="#userEditDialog"
               onclick="editUser(${row.userId})">修改</a>
            <a href="#" class="btn btn-danger btn-xs" onclick="deleteUser(${row.userId})">删除</a>
          </td>
        </tr>
      </c:forEach>
      </tbody>
    </table>
    <div class="col-md-12 text-right">
      <ujn:page url="${pageContext.request.contextPath }/user"/>
    </div>
  </div>
</div>

<!-- 创建模态框 -->
<div class="modal fade" id="newUserDialog" tabindex="-1" role="dialog"
     aria-labelledby="myModalLabel">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <h4 class="modal-title" id="myModalLabel1">新建用户</h4>
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body">
        <form class="form-horizontal" id="new_user_form">
          <div class="form-group">
            <label for="new_username" class="col-sm-3 control-label">
              用户名
            </label>
            <div class="col-sm-10">
              <input type="text" class="form-control" id="new_username" placeholder="用户名" name="username" />
            </div>
          </div>
          <div class="form-group">
            <label for="new_password" class="col-sm-2 control-label">密码</label>
            <div class="col-sm-10">
              <input type="password" class="form-control" id="new_password" placeholder="密码" name="password" />
            </div>
          </div>
          <div class="form-group">
            <label for="new_phone" class="col-sm-3 control-label">手机号</label>
            <div class="col-sm-10">
              <input type="text" class="form-control" id="new_phone" placeholder="手机号" name="phone" />
            </div>
          </div>
          <div class="form-group">
            <label for="new_userType" class="col-sm-4 control-label">用户类型</label>
            <div class="col-sm-10">
              <select class="form-control" id="new_userType" placeholder="用户类型" name="userType">
                <option value=""></option>
                <option value="普通用户">普通用户</option>
                <option value="VIP用户">VIP用户</option>
                <option value="管理员">管理员</option>
              </select>

            </div>
          </div>
        </form>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>

        <button type="button" class="btn btn-primary" onclick="createUser()">新增用户</button>

      </div>
    </div>
  </div>
</div>
<!-- 修改模态框 -->
<div class="modal fade" id="userEditDialog" tabindex="-1" role="dialog"
     aria-labelledby="myModalLabel">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
        <div class="modal-header">
            <h4 class="modal-title" id="myModalLabel2">修改用户信息</h4>
            <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                <span aria-hidden="true">&times;</span>
            </button>
        </div>
      <div class="modal-body">
        <form class="form-horizontal" id="edit_user_form">
          <input type="hidden" id="edit_userId" name="userId"/>
          <div class="form-group">
            <label for="edit_username" class="col-sm-4 control-label">用户名称</label>
            <div class="col-sm-10">
              <input type="text" class="form-control" id="edit_username" placeholder="用户名称" name="username" />
            </div>
          </div>

          <div class="form-group">
            <label for="edit_password" class="col-sm-2 control-label">密码</label>
            <div class="col-sm-10">
              <input type="text" class="form-control" id="edit_password" placeholder="密码" name="password" />
            </div>
          </div>
          <div class="form-group">
            <label for="edit_phone" class="col-sm-3 control-label">手机号</label>
            <div class="col-sm-10">
              <input type="text" class="form-control" id="edit_phone" placeholder="手机号" name="phone" />
            </div>
          </div>
          <div class="form-group">
            <label for="edit_userType" class="col-sm-4 control-label">用户状态</label>
            <div class="col-sm-10">
              <select class="form-control" id="edit_userType" placeholder="用户状态" name="new_userType">
                <option value="普通用户">普通用户</option>
                <option value="VIP用户">VIP用户</option>
                <option value="管理员">管理员</option>
              </select>
            </div>

          </div>
        </form>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
        <button type="button" class="btn btn-primary" onclick="updateUser()">保存修改</button>
      </div>
    </div>
  </div>
</div>
<%
} else{
%>
<!--登陆失败界面-->
<div class="jumbotron jumbotron-fluid">
  <div class="container">
    <h1 class="display-4">欢迎您,登录后即可查看</h1>
    <p class="lead">
      <a href="${pageContext.request.contextPath}/login">Welcome, log in to view</a>
    </p>
  </div>
</div>
<%  }
%>
<!-- 引入js文件 -->
<script src="<%=basePath%>js/jquery-1.11.3.min.js"></script>
<!-- Bootstrap Core JavaScript -->
<script src="<%=basePath%>js/bootstrap.js"></script>
<!-- Bootstrap-bundle JavaScript -->
<script src="<%=basePath%>js/bootstrap.bundle.js"></script>
<!-- Metis Menu Plugin JavaScript -->
<script src="<%=basePath%>js/metisMenu.min.js"></script>
<!-- DataTables JavaScript -->
<script src="<%=basePath%>js/jquery.dataTables.min.js"></script>
<script src="<%=basePath%>js/dataTables.bootstrap.min.js"></script>
<!-- Custom Theme JavaScript -->
<script src="<%=basePath%>js/sb-admin-2.js"></script>
<!-- 编写js代码 -->
<script type="text/javascript">
  //清空新建窗口中的数据
  function clearUser() {
    $("#new_username").val("");
    $("#new_password").val("")
    $("#new_phone").val("")
    $("#new_userType").val("")
  }

  // 创建
  function createUser() {
    $.post("<%=basePath%>user",
            $("#new_user_form").serialize(), function (data) {
              if (data == "OK") {
                alert("用户创建成功！");
                window.location.reload();
              } else {
                alert("用户创建失败！");
                window.location.reload();
              }
            });
  }

  // 通过id获取修改的用户信息
  function editUser(id) {
    $.ajax({
      type: "get",
      url: "<%=basePath%>user/" + id,
      success: function (data) {
        $("#edit_userId").val(data.userId);
        $("#edit_username").val(data.username);
        $("#edit_phone").val(data.phone);
        $("#edit_password").val(data.password)
        $("#edit_userType").val(data.userType)

      }
    });
  }

  // 执行修改操作
  function updateUser() {
    var userId = $("#edit_userId").val();
    var username = $("#edit_username").val();
    var phone = $("#edit_phone").val();
    var password = $("#edit_password").val();
    var userType = $("#edit_userType").val();
    $.ajax({
      url: '<%=basePath%>user',
      type: 'PUT',
      data: JSON.stringify({
        userId: userId,
        username: username,
        phone: phone,
        password: password,
        userType: userType
      }),
      contentType: "application/json;charset=UTF-8",
      success: function (data) {
        if (data == "OK") {
          alert("用户信息更新成功！");
          window.location.reload();
        } else {
          alert("用户信息更新失败！");
          window.location.reload();
        }
      }
    });
  }

  // 删除
  function deleteUser(id) {
    if (confirm('确实要删除该用户吗?')) {
      $.ajax({
        url: '<%=basePath%>user/' + id,
        type: 'DELETE',
        success: function (data) {
          if (data == "OK") {
            alert("用户信息删除成功！");
            window.location.reload();
          } else {
            alert("用户信息删除失败！");
            window.location.reload();
          }
        }
      });
    }
  }
</script>
</body>

</html>
